﻿using System;
using System.Text;
using System.Net;
using System.IO;

namespace AMing.Helper.WindowsPhone8
{
    public class PushHelper
    {
        public void SendToastMsg(string url, string postData, Action<string> callback)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            try
            {
                request.AllowAutoRedirect = true;
                request.Method = "POST";
                request.ContentType = "text/xml";
                request.Headers["X-MessageID"] = Guid.NewGuid().ToString();
                request.Headers["X-WindowsPhone-Target"] = "toast";
                request.Headers["X-NotificationClass"] = "2";

                if (!string.IsNullOrEmpty(postData))
                {
                    byte[] postdata = Encoding.UTF8.GetBytes(postData);
                    request.BeginGetRequestStream(async =>
                    {
                        try
                        {
                            using (Stream stream = request.EndGetRequestStream(async))
                            {
                                stream.Write(postdata, 0, postdata.Length);
                            }
                            request.BeginGetResponse(async2 =>
                            {
                                try
                                {
                                    WebResponse rep = request.EndGetResponse(async2);

                                    string notificationStatus = rep.Headers["X-NotificationStatus"];
                                    string notificationChannelStatus = rep.Headers["X-SubscriptionStatus"];
                                    string deviceConnectionStatus = rep.Headers["X-DeviceConnectionStatus"];

                                    string msg = notificationStatus + " | " + deviceConnectionStatus + " | " + notificationChannelStatus;

                                    callback(msg);
                                }
                                catch
                                {
                                    callback(null);
                                }
                            }, null);
                        }
                        catch
                        {
                            callback(null);
                        }
                    },
                    null);
                }
            }
            catch
            {
                callback(null);
            }
        }

        public void SendRawMsg(string url, string postData, Action<string> callback)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            try
            {
                request.AllowAutoRedirect = true;
                request.Method = "POST";
                request.ContentType = "text/xml";
                request.Headers["X-MessageID"] = Guid.NewGuid().ToString();
                request.Headers["X-NotificationClass"] = "3";

                if (!string.IsNullOrEmpty(postData))
                {
                    byte[] postdata = Encoding.UTF8.GetBytes(postData);
                    request.BeginGetRequestStream(async =>
                    {
                        try
                        {
                            using (Stream stream = request.EndGetRequestStream(async))
                            {
                                stream.Write(postdata, 0, postdata.Length);
                            }
                            request.BeginGetResponse(async2 =>
                            {
                                try
                                {
                                    WebResponse rep = request.EndGetResponse(async2);

                                    string notificationStatus = rep.Headers["X-NotificationStatus"];
                                    string notificationChannelStatus = rep.Headers["X-SubscriptionStatus"];
                                    string deviceConnectionStatus = rep.Headers["X-DeviceConnectionStatus"];

                                    string msg = notificationStatus + " | " + deviceConnectionStatus + " | " + notificationChannelStatus;

                                    callback(msg);
                                }
                                catch
                                {
                                    callback(null);
                                }
                            }, null);
                        }
                        catch
                        {
                            callback(null);
                        }
                    },
                    null);
                }
            }
            catch
            {
                callback(null);
            }
        }

        public void SendTokenMsg(string url, string postData, Action<string> callback)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            try
            {
                request.AllowAutoRedirect = true;
                request.Method = "POST";
                request.ContentType = "text/xml";
                request.Headers["X-MessageID"] = Guid.NewGuid().ToString();
                request.Headers["X-WindowsPhone-Target"] = "token";
                request.Headers["X-NotificationClass"] = "1";

                if (!string.IsNullOrEmpty(postData))
                {
                    byte[] postdata = Encoding.UTF8.GetBytes(postData);
                    request.BeginGetRequestStream(async =>
                    {
                        try
                        {
                            using (Stream stream = request.EndGetRequestStream(async))
                            {
                                stream.Write(postdata, 0, postdata.Length);
                            }
                            request.BeginGetResponse(async2 =>
                            {
                                try
                                {
                                    WebResponse rep = request.EndGetResponse(async2);

                                    string notificationStatus = rep.Headers["X-NotificationStatus"];
                                    string notificationChannelStatus = rep.Headers["X-SubscriptionStatus"];
                                    string deviceConnectionStatus = rep.Headers["X-DeviceConnectionStatus"];

                                    string msg = notificationStatus + " | " + deviceConnectionStatus + " | " + notificationChannelStatus;

                                    callback(msg);
                                }
                                catch
                                {
                                    callback(null);
                                }
                            }, null);
                        }
                        catch
                        {
                            callback(null);
                        }
                    },
                    null);
                }
            }
            catch
            {
                callback(null);
            }
        }
    }
}
